package cn.itcast.nems.journal.service.impl;

import cn.itcast.nems.base.dao.entity.E2Clazz;
import cn.itcast.nems.base.dao.mapper.E2ClazzMapper;
import cn.itcast.nems.common.account.AccountHolder;
import cn.itcast.nems.common.tenant.TenantInfo;
import cn.itcast.nems.journal.dto.JournalTransferCreateBo;
import cn.itcast.nems.journal.dto.JournalTransferDelayCreateBo;
import cn.itcast.nems.journal.service.JournalTransferCommonService;
import cn.itcast.nems.journal.service.JournalTransferDelayService;
import cn.itcast.nems.journal.service.JournalTransferReasonServiceV2;
import cn.itcast.nems.journal.service.JournalTransferService;
import cn.itcast.nems.product.scheduler.TuitionFeeService;
import cn.itcast.nems.studentrecord.dao.entity.JournalTransferLine;
import cn.itcast.nems.studentrecord.dao.entity.JournalTransferType;
import cn.itcast.nems.studentrecord.dao.mapper.StudentRecordMapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import lombok.RequiredArgsConstructor;
import org.springframework.stereotype.Service;
import org.springframework.util.Assert;

import java.math.BigDecimal;
import java.time.LocalDateTime;

@Service
@RequiredArgsConstructor
public class JournalTransferDelayServiceImpl implements JournalTransferDelayService {

    private final StudentRecordMapper studentRecordMapper;
    private final E2ClazzMapper e2ClazzMapper;
    private final JournalTransferReasonServiceV2 journalTransferReasonService;
    private final JournalTransferService journalTransferService;
    private final TuitionFeeService tuitionFeeService;
    private final JournalTransferCommonService commonService;

    /**
     * 待定班学员-详情-转入确定班-提交按钮
     */
    @Override
    public String createJournalTransferDelay(JournalTransferDelayCreateBo createBo) {
        var studentRecord = studentRecordMapper.selectById(createBo.getOriginStudentRecordId());
        var sourceClazz = e2ClazzMapper.selectOne(new QueryWrapper<E2Clazz>()
                .lambda().eq(E2Clazz::getClazzId, studentRecord.getClazzId()));
        var targetClazz = e2ClazzMapper.selectOne(new QueryWrapper<E2Clazz>()
                .lambda().eq(E2Clazz::getClazzId, createBo.getTargetClazzId()));
        var reason = journalTransferReasonService.findReasonByCodeOrNull(createBo.getReasonCode());
        var targetClazzOriginalAmount = tuitionFeeService.findTuition(targetClazz.getClazzId(), null).multiply(new BigDecimal(100)).intValue();

        var bo = new JournalTransferCreateBo();
        bo.setType(JournalTransferType.DELAY);
        bo.setState(createBo.getState());
        bo.setTargetClazzCampusId(targetClazz.getSchoolCode());
        bo.setTargetClazzModelId(targetClazz.getTeachingModel());
        bo.setSpecialFlag(createBo.getSpecialFlag());
        bo.setRemark(createBo.getRemark());
        bo.setTargetClazzSubjectId(targetClazz.getSubjectId());
        bo.setTargetClazzId(targetClazz.getClazzId());
        bo.setReasonCode(createBo.getReasonCode());
        bo.setDirection(createBo.getDirection());
        var journalTransferLine = new JournalTransferLine();
        final var settlementResult = commonService.calculate(
                JournalTransferType.DELAY,
                studentRecord,
                createBo.getTargetClazzInDate().minusDays(1).atTime(0, 0),
                sourceClazz
        );
        Assert.notNull(createBo.getOriginClazzOutDate(), "原班级出班日期不能为空");
        journalTransferLine.setOriginClazzOutDate(createBo.getOriginClazzOutDate());
        journalTransferLine.setTargetClazzInDate(createBo.getTargetClazzInDate());
        journalTransferLine.setOriginStudentRecordId(studentRecord.getId());
        journalTransferLine.setOriginClazzId(sourceClazz.getClazzId());
        journalTransferLine.setOriginClazzName(sourceClazz.getClazzName());
        journalTransferLine.setOriginClazzInDate(studentRecord.getInDate().toLocalDate());
        journalTransferLine.setOriginClazzOriginalAmount(studentRecord.getPrice());
        journalTransferLine.setOriginClazzDiscountAmount(studentRecord.getDiscountAmount());
        journalTransferLine.setOriginClazzAmountDue(studentRecord.getAmountDue());
        journalTransferLine.setOriginActualAmountDue(studentRecord.getActualAmountDue());
        journalTransferLine.setOriginClazzSettlementAmount(settlementResult.getSettlementAmount());
        journalTransferLine.setOriginClazzActualSettlementAmount(settlementResult.getSettlementAmount());
        journalTransferLine.setOriginClazzServiceDays(settlementResult.getServiceDays());
        journalTransferLine.setOriginClazzSettlementDays(settlementResult.getSettlementDays());
        journalTransferLine.setOriginClazzActualSettlementDays(settlementResult.getSettlementDays());
        journalTransferLine.setTargetClazzOriginalAmount(targetClazzOriginalAmount);
        journalTransferLine.setTargetClazzDiscountAmount(0);
        journalTransferLine.setTargetClazzAmountDue(studentRecord.getActualAmountDue() - settlementResult.getSettlementAmount());
        journalTransferLine.setTargetClazzActualAmountDue(studentRecord.getActualAmountDue() - settlementResult.getSettlementAmount());
        journalTransferLine.setTargetClazzTransferAmount(settlementResult.getTransferAmount());
        journalTransferLine.setPriceDifferenceFlag(Boolean.TRUE);
        journalTransferLine.setPriceDifferenceAmount(studentRecord.getPrice());
        journalTransferLine.setTenantId(TenantInfo.DEFAULT_TENANT_ID);
        journalTransferLine.setSubtenantId(TenantInfo.DEFAULT_SUBTENANT_ID);
        journalTransferLine.setCreatedBy(AccountHolder.getRequiredAccount());
        journalTransferLine.setCreatedDatetime(LocalDateTime.now());
        journalTransferLine.setDeletedFlag(Boolean.FALSE);
        if (reason != null) {
            journalTransferLine.setReasonCode(reason.getCode());
            journalTransferLine.setReasonDescription(reason.getName());
        }
        bo.setSingleLine(journalTransferLine);
        return journalTransferService.create(bo);
    }

}
